package com.hivekion.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hivekion.common.core.SystemConstant;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.common.redis.RedisUtil;
import com.hivekion.system.domain.*;
import com.hivekion.system.domain.vo.*;
import com.hivekion.system.mapper.*;
import com.hivekion.system.service.ISysStatisticsBiSchameService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysStatisticsBiSchameServiceImpl implements ISysStatisticsBiSchameService {

    @Autowired
    private SysStatisticsTargetSchameMapper targetSchameMapper;

    @Autowired
    private SysStatisticsItemSchameMapper itemSchameMapper;

    @Autowired
    private SysStatisticsTargetSumMapper targetSumMapper;

    @Autowired
    private SysStatisticsTargetMapper targetMapper;

    @Autowired
    private SysStatisticsItemMapper itemMapper;

    @Autowired
    private SysStatisticsBiNameMapper biNameMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Boolean checkItemSchameNameUnique(String itemSchameName, String itemSchameId) {

        LambdaQueryWrapper<SysStatisticsItemSchame> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysStatisticsItemSchame::getItemId, itemSchameName);
        if (StringUtils.isNotEmpty(itemSchameId)) {
            wrapper.eq(SysStatisticsItemSchame::getId, itemSchameId);
        }
        List<SysStatisticsItemSchame> list = itemSchameMapper.selectList(wrapper);
        return list.size() > 0;
    }

    @Override
    public Boolean checkSchameSumSchameUnique(String itemSchameName, String itemSchameId) {

        LambdaQueryWrapper<SysStatisticsTargetSum> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysStatisticsTargetSum::getName, itemSchameName);
        if (StringUtils.isNotEmpty(itemSchameId)) {
            wrapper.eq(SysStatisticsTargetSum::getId, itemSchameId);
        }
        List<SysStatisticsTargetSum> list = targetSumMapper.selectList(wrapper);
        return list.size() > 0;
    }

    @Override
    public List<SysStatisticsTargetSum> getTargetSumSchameByName(String targetSumName) {

        LambdaQueryWrapper<SysStatisticsTargetSum> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysStatisticsTargetSum::getName, targetSumName);

        return targetSumMapper.selectList(wrapper);
    }

    @Override
    public boolean createItemSchame(SysStatisticsItemSchameCreateInputVo inputVo) {

        String itemIdStr = inputVo.getItemId();
        if (itemIdStr == null || itemIdStr.equals("")) {
            throw new BusinessException(500, "itemId不能为空");
        }
        boolean bl = true;
        for (String itemId : itemIdStr.split(",")) {
            SysStatisticsItemSchame item = new SysStatisticsItemSchame();
            item.setBiNameId(inputVo.getBiNameId());
            item.setItemId(itemId);
//		  item.setYear(inputVo.getYear());
            boolean bl2 = itemSchameMapper.insert(item) > 0;
            if (bl2) this.updateRedis("item");
            bl = bl && bl2;
        }

        return bl;
    }

    @Override
    public SysStatisticsItemSchame getItemInfo(String itemSchameId) {
        SysStatisticsItemSchame itemSchame = itemSchameMapper.selectById(itemSchameId);
        if (itemSchame == null) {
            throw new BusinessException(500, "未找到指标信息");
        }
        return itemSchame;
    }

    @Override
    public boolean deleteItemSchame(String itemSchameId) {

        boolean bl = itemSchameMapper.deleteById(itemSchameId) > 0;
        if (bl) this.updateRedis("item");
        return bl;
    }

    @Override
    public List<SysStatisticsItemSchameSelectViewVo> getItemSchameSelectList(String biNameId) {
        List<SysStatisticsItemSchame> list = getAllItemSchameList();
        List<SysStatisticsItemSchameSelectViewVo> nodes = new ArrayList<>();
        Map<String, SysStatisticsItemSchame> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            list.forEach(
                    item -> {
                        String itemId = item.getItemId();
                        if (itemId != null && biNameId != null) {
                            if (item.getBiNameId() != null && item.getBiNameId().equals(biNameId)) {
                                map.put(item.getItemId(), item);
                            }
                        }
                    });
        }
        if (map.keySet().size() == 0) {
            return nodes;
        }
        LambdaQueryWrapper<SysStatisticsItem> wrapper = Wrappers.lambdaQuery();
        for (String itemId : map.keySet()) {
            wrapper.eq(SysStatisticsItem::getId, itemId).or();
        }
        List<SysStatisticsItem> sysStatisticsItems = itemMapper.selectList(wrapper);

        for (SysStatisticsItem statisticsItem : sysStatisticsItems) {
            SysStatisticsItemSchameSelectViewVo vo = new SysStatisticsItemSchameSelectViewVo();
            if (statisticsItem != null
                    && statisticsItem.getId() != null
                    && map.get(statisticsItem.getId()) != null) {

                BeanUtils.copyProperties(map.get(statisticsItem.getId()), vo);
                BeanUtils.copyProperties(statisticsItem, vo);
                vo.setId(map.get(statisticsItem.getId()).getId());
                nodes.add(vo);
            }
        }

        return nodes;
    }

    @Override
    public List<SysBiDataItemSchameVo> getItemSchameList(String biNameId) {
        List<SysStatisticsItem> allItemList = getAllItemList();
        LambdaQueryWrapper<SysStatisticsItemSchame> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysStatisticsItemSchame::getBiNameId, biNameId);
        List<SysStatisticsItemSchame> itemSchames = itemSchameMapper.selectList(wrapper);

        List<SysBiDataItemSchameVo> voList = new ArrayList<>();
        if (itemSchames != null && itemSchames.size() > 0) {
            List<String> itemIds = itemSchames.stream().map(x -> x.getItemId()).collect(Collectors.toList());
            List<SysStatisticsItem> selectItems = allItemList.stream()
                    .filter(x -> itemIds.contains(x.getId())).collect(Collectors.toList());
            List<SysStatisticsItem> parentNode = selectItems.stream().filter(x -> x.getParentId() == null || "".equals(x.getParentId())).collect(Collectors.toList());
            if (parentNode != null && parentNode.size() > 0) {
                parentNode.stream().sorted(Comparator.comparing(SysStatisticsItem::getSortCode))
                        .forEach(item -> {
                            SysBiDataItemSchameVo vo = new SysBiDataItemSchameVo();
                            vo.setTitle(item.getName());
                            vo.setItemId(item.getId());
                            vo.setItemName(item.getName());
                            vo.setChildren(getItemChildren(selectItems, item.getId(), biNameId));
                            voList.add(vo);
                        });
            }

        }
        return voList;
    }


    private List<SysBiDataItemSchameVo> getItemChildren(List<SysStatisticsItem> list, String parentId, String biNameId) {
        List<SysBiDataItemSchameVo> voList = new ArrayList<>();
        List<SysStatisticsItem> parentNode = list.stream()
                .filter(x -> parentId.equals(x.getParentId())).collect(Collectors.toList());
        if (parentNode != null && parentNode.size() > 0) {
            parentNode.stream().sorted(Comparator.comparing(SysStatisticsItem::getSortCode))
                    .forEach(item -> {
                        SysBiDataItemSchameVo vo = new SysBiDataItemSchameVo();
                        vo.setItemId(item.getId());
                        vo.setTitle(item.getName());
                        vo.setItemName(item.getName());
                        List<SysBiDataItemSchameVo> child = getItemChildren(list, item.getId(), biNameId);
                        vo.setChildren(child);
                        voList.add(vo);
                    });
        }
        return voList;
    }

    //统计表单配置树加载
    @Override
    public List<SysBiDataItemSchameVo> getSettingItemSchameList(String biNameId) {
        List<SysStatisticsItem> allItemList = getAllItemList();
        LambdaQueryWrapper<SysStatisticsItemSchame> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysStatisticsItemSchame::getBiNameId, biNameId);
        List<SysStatisticsItemSchame> itemSchames = itemSchameMapper.selectList(wrapper);

        List<SysBiDataItemSchameVo> voList = new ArrayList<>();
        if (itemSchames != null && itemSchames.size() > 0) {
            List<String> itemIds = itemSchames.stream().map(x -> x.getItemId()).collect(Collectors.toList());
            List<SysStatisticsItem> selectItems = allItemList.stream()
                    .filter(x -> itemIds.contains(x.getId())).collect(Collectors.toList());
            List<SysStatisticsItem> parentNode = selectItems.stream().filter(x -> x.getParentId() == null || "".equals(x.getParentId())).collect(Collectors.toList());
            if (parentNode != null && parentNode.size() > 0) {
                parentNode.stream().sorted(Comparator.comparing(SysStatisticsItem::getSortCode))
                        .forEach(item -> {
                            SysBiDataItemSchameVo vo = new SysBiDataItemSchameVo();
                            LambdaQueryWrapper<SysStatisticsItemSchame> schameWrapper = Wrappers.lambdaQuery();
                            schameWrapper.eq(SysStatisticsItemSchame::getItemId, item.getId());
                            schameWrapper.eq(SysStatisticsItemSchame::getBiNameId, biNameId);
                            List<SysStatisticsItemSchame> sysStatisticsItems = itemSchameMapper.selectList(schameWrapper);
                            if (sysStatisticsItems != null && sysStatisticsItems.size() > 0) {
//                                vo.setKey(sysStatisticsItems.get(0).getId());
                                vo.setItemId(sysStatisticsItems.get(0).getId());
                            }
                            vo.setTitle(item.getName());

                            vo.setItemName(item.getName());
                            vo.setChildren(getSettingItemChildren(selectItems, item.getId(), biNameId));
                            voList.add(vo);
                        });
            }

        }
        return voList;
    }

    //统计表单配置子树加载
    private List<SysBiDataItemSchameVo> getSettingItemChildren(List<SysStatisticsItem> list, String parentId, String biNameId) {
        List<SysBiDataItemSchameVo> voList = new ArrayList<>();
        List<SysStatisticsItem> parentNode = list.stream()
                .filter(x -> parentId.equals(x.getParentId())).collect(Collectors.toList());
        if (parentNode != null && parentNode.size() > 0) {
            parentNode.stream().sorted(Comparator.comparing(SysStatisticsItem::getSortCode))
                    .forEach(item -> {
                        SysBiDataItemSchameVo vo = new SysBiDataItemSchameVo();
                        LambdaQueryWrapper<SysStatisticsItemSchame> schameWrapper = Wrappers.lambdaQuery();
                        schameWrapper.eq(SysStatisticsItemSchame::getItemId, item.getId());
                        schameWrapper.eq(SysStatisticsItemSchame::getBiNameId, biNameId);
                        List<SysStatisticsItemSchame> sysStatisticsItems = itemSchameMapper.selectList(schameWrapper);
                        if (sysStatisticsItems != null && sysStatisticsItems.size() > 0) {
//                            vo.setKey(sysStatisticsItems.get(0).getId());
                            vo.setItemId(sysStatisticsItems.get(0).getId());
                        }
                        vo.setTitle(item.getName());
                        vo.setItemName(item.getName());
                        List<SysBiDataItemSchameVo> child = getItemChildren(list, item.getId(), biNameId);
                        vo.setChildren(child);
                        voList.add(vo);
                    });
        }
        return voList;
    }

    @Override
    public boolean createTargetSchame(SysStatisticsTargetSchameCreateInputVo inputVo) {
        SysStatisticsTargetSchame item = new SysStatisticsTargetSchame();
        BeanUtils.copyProperties(inputVo, item);
        item.setTargetType("sum");
        return targetSchameMapper.insert(item) > 0;
    }

    @Override
    public SysStatisticsTargetSchame getTargetInfo(String targetSchameId) {
        SysStatisticsTargetSchame targetSchame = targetSchameMapper.selectById(targetSchameId);
        if (targetSchame == null) {
            throw new BusinessException(500, "未找到指标信息");
        }
        return targetSchame;
    }

    // 已经选中的schame
    @Override
    public List<SysStatisticsTargetSchameSelectViewVo> getTargetSchameSelectList(
            String biNameId, String targetType) {
        List<SysStatisticsTargetSchame> list = getAllTargetSchameList();
        List<SysStatisticsTargetSchameSelectViewVo> nodes = new ArrayList<>();
        Map<String, SysStatisticsTargetSchame> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            list.forEach(
                    item -> {
                        if (item.getBiNameId() != null
                                && item.getBiNameId().equals(biNameId)
                                && item.getTargetType() != null
                                && item.getTargetType().equals(targetType)) {
                            map.put(item.getTargetId(), item);
                        }
                    });

            if (map.keySet().size() == 0) {
                return nodes;
            }
            if (targetType.equals("sum")) {
                LambdaQueryWrapper<SysStatisticsTargetSum> wrapper = Wrappers.lambdaQuery();
                for (String itemId : map.keySet()) {
                    wrapper.eq(SysStatisticsTargetSum::getId, itemId).or();
                }
                List<SysStatisticsTargetSum> statisticsTargetList =
                        targetSumMapper.selectList(wrapper);

                for (SysStatisticsTargetSum statisticsTarget : statisticsTargetList) {
                    SysStatisticsTargetSchame item = map.get(statisticsTarget.getId());
                    if (item != null) {
                        if (item.getTargetType().equals(targetType)) {
                            SysStatisticsTargetSchameSelectViewVo vo =
                                    new SysStatisticsTargetSchameSelectViewVo();
                            BeanUtils.copyProperties(statisticsTarget, vo);
                            BeanUtils.copyProperties(item, vo);
                            nodes.add(vo);
                        }
                    }
                }
            } else {

                LambdaQueryWrapper<SysStatisticsTarget> wrapper = Wrappers.lambdaQuery();
                for (String itemId : map.keySet()) {
                    wrapper.eq(SysStatisticsTarget::getId, itemId).or();
                }
                List<SysStatisticsTarget> statisticsTargetList = targetMapper.selectList(wrapper);

                for (SysStatisticsTarget statisticsTarget : statisticsTargetList) {
                    SysStatisticsTargetSchame item = map.get(statisticsTarget.getId());
                    if (item != null) {
                        if (item.getTargetType().equals(targetType)) {
                            SysStatisticsTargetSchameSelectViewVo vo =
                                    new SysStatisticsTargetSchameSelectViewVo();
                            BeanUtils.copyProperties(statisticsTarget, vo);
                            BeanUtils.copyProperties(item, vo);
                            nodes.add(vo);
                        }
                    }
                }
            }
        }
        return nodes;
    }

    // 可选中的列表
    @Override
    public List<SysStatisticsTargetSchameSelectViewVo> getTargetSchameList(
            String biNameId, String targetType) {
        List<SysStatisticsTarget> collect = getAllTargetList();
        List<SysStatisticsTargetSchameSelectViewVo> nodes = new ArrayList<>();

        if (collect.size() > 0) {
            collect.forEach(
                    item -> {
                        // 展示全部
                        SysStatisticsTargetSchameSelectViewVo vo =
                                new SysStatisticsTargetSchameSelectViewVo();
                        BeanUtils.copyProperties(item, vo);
                        vo.setId(null);
                        vo.setTargetId(item.getId());
                        vo.setTargetType(targetType);
                        nodes.add(vo);
                    });
        }
        return nodes;
    }

    @Override
    public List<SysStatisticsTargetSchameViewVo> getTargetSchameList(String biNameId) {
        LambdaQueryWrapper<SysStatisticsTargetSchame> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysStatisticsTargetSchame::getBiNameId, biNameId);
        List<SysStatisticsTargetSchame> list = targetSchameMapper.selectList(queryWrapper);
        List<SysStatisticsTargetSchameViewVo> viewList = new ArrayList<>();

        List<SysStatisticsTarget> allTargetList = getAllTargetList();
        if (list != null && list.size() > 0) {
            List<SysStatisticsTargetSchame> parentList = list.stream().filter(x -> x.getParentId() == null || ("".equals(x.getParentId())))
                    .collect(Collectors.toList());
            if (parentList != null && parentList.size() > 0) {
                parentList.stream().sorted(Comparator.comparing(SysStatisticsTargetSchame::getSortCode)).forEach(item -> {
                    SysStatisticsTargetSchameViewVo vo = new SysStatisticsTargetSchameViewVo();
                    vo.setKey(item.getId());
                    vo.setTitle(item.getTargetName());
                    vo.setChildren(getTargetSchameChildren(list, item.getId(), allTargetList));
                    Optional<SysStatisticsTarget> first = allTargetList.stream().filter(x -> item.getTargetId().equals(x.getId())).findFirst();
                    if (first.isPresent()) {
                        vo.setTargetName(first.get().getName());
                        BeanUtils.copyProperties(first.get(), vo);
                    }
                    BeanUtils.copyProperties(item, vo);
                    viewList.add(vo);
                });
            }
        }
        return viewList;
    }

    @Override
    public List<SysStatisticsTargetSchame> getTargetSchames(String biNameId) {
        LambdaQueryWrapper<SysStatisticsTargetSchame> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysStatisticsTargetSchame::getBiNameId, biNameId);
        List<SysStatisticsTargetSchame> list = targetSchameMapper.selectList(queryWrapper);

        return list;
    }

    @Override
    public SysStatisticsTargetSchameModelVo getTargetSchameInfo(String key) {
        SysStatisticsTargetSchame model = targetSchameMapper.selectById(key);
        if (model == null) {
            throw new BusinessException(500, "未找到数据");
        }
        SysStatisticsTargetSchameModelVo vo = new SysStatisticsTargetSchameModelVo();
        BeanUtils.copyProperties(model, vo);
        // 查询指标分类
        Optional<SysStatisticsTarget> first = getAllTargetList().stream()
                .filter(x -> model.getTargetId().equals(x.getId())).findFirst();
        if (first.isPresent()) {
            vo.setTargetType(first.get().getTargetType());
        }
        return vo;
    }

    @Override
    public boolean editTargetSchame(SysStatisticsTargetSchameUpdateVo vo) {
        SysStatisticsTargetSchame model = targetSchameMapper.selectById(vo.getId());
        if (model == null) {
            throw new BusinessException(500, "未找到数据");
        }
        BeanUtils.copyProperties(vo, model);
        return targetSchameMapper.updateById(model) > 0;
    }

    private List<SysStatisticsTargetSchameViewVo> getTargetSchameChildren(List<SysStatisticsTargetSchame> list, String parentId, List<SysStatisticsTarget> allTargetList) {
        List<SysStatisticsTargetSchameViewVo> viewList = new ArrayList<>();
        list.stream().filter(x -> parentId.equals(x.getParentId()))
                .sorted(Comparator.comparing(SysStatisticsTargetSchame::getSortCode))
                .forEach(item -> {
                    SysStatisticsTargetSchameViewVo vo = new SysStatisticsTargetSchameViewVo();
                    vo.setKey(item.getId());
                    vo.setTitle(item.getTargetName());
                    List<SysStatisticsTargetSchameViewVo> children = getTargetSchameChildren(list, item.getId(), allTargetList);
                    vo.setChildren(children);
                    Optional<SysStatisticsTarget> first = allTargetList.stream().filter(x -> item.getTargetId().equals(x.getId())).findFirst();
                    if (first.isPresent()) {
                        vo.setTargetName(first.get().getName());
                        BeanUtils.copyProperties(first.get(), vo);
                    }
                    BeanUtils.copyProperties(item, vo);
                    viewList.add(vo);
                });
        return viewList;
    }

    @Override
    public boolean deleteTargetSchame(String targetSchameId) {
        LambdaQueryWrapper<SysStatisticsTargetSchame> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysStatisticsTargetSchame::getParentId, targetSchameId);

        if (targetSchameMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException(500, "删除节点下有子集，请先删除子集节点！");
        }
        return targetSchameMapper.deleteById(targetSchameId) > 0;
    }

    @Override
    public List<SysStatisticsTargetSumSchameSelectViewVo> getTargetSumSchameList(String biNameId) {
        List<SysStatisticsTargetSchameSelectViewVo> list =
                getTargetSchameSelectList(biNameId, "sum");
        List<SysStatisticsTargetSumSchameSelectViewVo> nodes = new ArrayList<>();

        Map<String, SysStatisticsTargetSchameSelectViewVo> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            list.forEach(
                    item -> {
                        if (item.getTargetType() != null && item.getTargetType().equals("sum")) {
                            map.put(item.getTargetId(), item);
                        }
                    });
            if (map.keySet().size() == 0) {
                return nodes;
            }
            LambdaQueryWrapper<SysStatisticsTargetSum> wrapper = Wrappers.lambdaQuery();
            for (String itemId : map.keySet()) {
                wrapper.eq(SysStatisticsTargetSum::getId, itemId).or();
            }
            List<SysStatisticsTargetSum> statisticsTargetList = targetSumMapper.selectList(wrapper);

            for (SysStatisticsTargetSum targetSum : statisticsTargetList) {
                SysStatisticsTargetSchameSelectViewVo item = map.get(targetSum.getId());
                if (item != null) {
                    SysStatisticsTargetSumSchameSelectViewVo vo =
                            new SysStatisticsTargetSumSchameSelectViewVo();
                    BeanUtils.copyProperties(item, vo);
                    BeanUtils.copyProperties(targetSum, vo);
                    vo.setId(item.getId());
                    nodes.add(vo);
                }
            }
        }
        return nodes;
    }

    @Override
    public SysStatisticsTargetSum getTargetSumInfo(String targetSumId) {

        SysStatisticsTargetSum targetSum = targetSumMapper.selectById(targetSumId);
        if (targetSum == null) {
            throw new BusinessException(500, "未找到指标信息");
        }
        return targetSum;
    }

    @Override
    public boolean addTargetSum(SysStatisticsTargetSumSchameCreateInputVo inputVo) {

        // TODO 单独新增计算指标id
        //        boolean isok = this.checkSchameSumSchameUnique(inputVo.getName(),"");
        //        if(isok){
        //            throw new BusinessException(500, "计算指标名称已存在");
        //        }
        SysStatisticsTargetSum targetSum = new SysStatisticsTargetSum();
        BeanUtils.copyProperties(inputVo, targetSum);
        final String uuid = UUID.randomUUID().toString().replace("-", "");
        targetSum.setId(uuid);
        // TODO 生成Sum记录表
        boolean bl = targetSumMapper.insert(targetSum) > 0;
        if (bl) this.updateRedis("sum");
        // TODO
        if (bl) {
            SysStatisticsTargetSum list = targetSumMapper.selectById(targetSum.getId());
            SysStatisticsTargetSchame targetSchame = new SysStatisticsTargetSchame();
            if (list == null) {
                throw new BusinessException(500, "增加TargetSumSchame 失败");
            }
            BeanUtils.copyProperties(list, targetSchame);
            targetSchame.setTargetId(list.getId());
//		  targetSchame.setTargetType("sum");
            targetSchame.setId(null);
            bl = targetSchameMapper.insert(targetSchame) > 0;
            if (bl) this.updateRedis("target");
        }
        return bl;
    }

    @Override
    public boolean editTargetSum(SysStatisticsTargetSumUpdateInputVo inputVo) {
        SysStatisticsTargetSum list = targetSumMapper.selectById(inputVo.getId());
        if (list == null) {
            throw new BusinessException(500, "更新TargetSum 不存在");
        }
        BeanUtils.copyProperties(inputVo, list);
        boolean bl = targetSumMapper.updateById(list) > 0;
        if (bl) this.updateRedis("sum");
        return bl;
    }

    @Override
    public boolean addTargetSumSchame(String biNameId, String targetSumId) {
        SysStatisticsTargetSum list = targetSumMapper.selectById(targetSumId);
        SysStatisticsTargetSchame targetSchame = new SysStatisticsTargetSchame();

        BeanUtils.copyProperties(list, targetSchame);
        targetSchame.setTargetId(list.getId());
//	    targetSchame.setTargetType("sum");
        targetSchame.setBiNameId(biNameId);
        targetSchame.setId(null);
        boolean bl = targetSchameMapper.insert(targetSchame) > 0;
        if (bl) {
            this.updateRedis("target");
        }
        return bl;
    }

    @Override
    public List<SysStatisticsTargetSum> getTargetSumList() {
        return getAllTargetSumList();
    }

    @Override
    public boolean updateBiNameCondtion(SysStatisticsBiNameConditionUpdateVo inputVo) {
        SysStatisticsBiName biName = getTargetClassic(inputVo.getBiNameId());
        BeanUtils.copyProperties(inputVo, biName);
        boolean bl = biNameMapper.updateById(biName) > 0;
        return bl;
    }

    @Override
    public SysStatisticsBiName getBiNameCondtion(String biNameId) {
        SysStatisticsBiName bl = biNameMapper.selectById(biNameId);
        if (bl == null) {
            throw new BusinessException(500, "未找到指标信息");
        }
        return bl;
    }

    private SysStatisticsBiName getTargetClassic(String biNameId) {
        SysStatisticsBiName biName = biNameMapper.selectById(biNameId);
        if (biName == null) {
            throw new BusinessException(500, "未找到报表名信息");
        }
        return biName;
    }

    /*
     * redis 缓存数据
     * */
    public List<SysStatisticsItem> getAllItemList() {

        if (redisUtil.hasKey(SystemConstant.redis_item_data)) {
            List<SysStatisticsItem> list =
                    JSON.parseArray(
                            redisUtil.get(SystemConstant.redis_item_data).toString(),
                            SysStatisticsItem.class);
            return list;
        } else {
            List<SysStatisticsItem> allBiName = itemMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_item_data, JSON.toJSONString(allBiName));
            return allBiName;
        }
    }

    /*
     * redis 缓存数据
     * */
    public List<SysStatisticsItemSchame> getAllItemSchameList() {

        if (redisUtil.hasKey(SystemConstant.redis_item_schame_data)) {
            List<SysStatisticsItemSchame> list =
                    JSON.parseArray(
                            redisUtil.get(SystemConstant.redis_item_schame_data).toString(),
                            SysStatisticsItemSchame.class);
            return list;
        } else {
            List<SysStatisticsItemSchame> allBiName = itemSchameMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_item_schame_data, JSON.toJSONString(allBiName));
            return allBiName;
        }
    }

    /*
     * redis 缓存数据
     * */
    public List<SysStatisticsTargetSchame> getAllTargetSchameList() {

//        if (redisUtil.hasKey(SystemConstant.redis_target_schame_data)) {
//            List<SysStatisticsTargetSchame> list =
//                    JSON.parseArray(
//                            redisUtil.get(SystemConstant.redis_target_schame_data).toString(),
//                            SysStatisticsTargetSchame.class);
//            return list;
//        } else {
            List<SysStatisticsTargetSchame> allTargetSchame = targetSchameMapper.selectList(null);
            redisUtil.set(
                    SystemConstant.redis_target_schame_data, JSON.toJSONString(allTargetSchame));
            return allTargetSchame;
//        }
    }

    public List<SysStatisticsTargetSum> getAllTargetSumList() {
        if (redisUtil.hasKey(SystemConstant.redis_target_sum_data)) {
            List<SysStatisticsTargetSum> list =
                    JSON.parseArray(
                            redisUtil.get(SystemConstant.redis_target_sum_data).toString(),
                            SysStatisticsTargetSum.class);
            return list;
        } else {
            List<SysStatisticsTargetSum> allTargetSum = targetSumMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_target_sum_data, JSON.toJSONString(allTargetSum));
            return allTargetSum;
        }
    }

    public List<SysStatisticsTarget> getAllTargetList() {
        if (redisUtil.hasKey(SystemConstant.redis_target_data)) {
            List<SysStatisticsTarget> list =
                    JSON.parseArray(
                            redisUtil.get(SystemConstant.redis_target_data).toString(),
                            SysStatisticsTarget.class);
            return list;
        } else {
            List<SysStatisticsTarget> allTarget = targetMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_target_data, JSON.toJSONString(allTarget));
            return allTarget;
        }
    }

    private void updateRedis(String type) {
        // 更新itemSchame
        if (type.equals("item")) {
            List<SysStatisticsItemSchame> allItemSchame = itemSchameMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_item_schame_data, JSON.toJSONString(allItemSchame));
            // 更新sum 指标
        } else if (type.equals("sum")) {
            List<SysStatisticsTargetSum> allTargetSum = targetSumMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_target_sum_data, JSON.toJSONString(allTargetSum));
        } else {
            // 更新targetSchame
            List<SysStatisticsTargetSchame> allTargetSchame = targetSchameMapper.selectList(null);
            redisUtil.set(
                    SystemConstant.redis_target_schame_data, JSON.toJSONString(allTargetSchame));
        }
    }
}
